package com.iot.framework.rabbitmq.service;


import com.alibaba.fastjson2.JSONObject;
import com.iot.common.enums.ProductGoodType;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.DeviceState;
import com.iot.system.domain.DeviceYields;
import com.iot.system.service.IDeviceDetailsService;
import com.iot.system.service.IDeviceStateService;
import com.iot.system.service.IDeviceYieldsService;
import com.iot.system.vo.DeviceYieldVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Component
public class DeviceYieldService {


    @Resource
    private IDeviceDetailsService iDeviceDetailsService;

    @Resource
    private IDeviceYieldsService iDeviceYieldsService;


    @Async
    public void deviceYield(String message) {
        DeviceYieldVo deviceYieldVo = JSONObject.parseObject(message, DeviceYieldVo.class);
        DeviceDetails deviceDetails = iDeviceDetailsService.selectDeviceDetailsByToken(deviceYieldVo.getCredentials());
        if(Objects.isNull(deviceDetails)){
            return;
        }

        //获取设备id
        DeviceYields deviceYields = new DeviceYields();
        deviceYields.setDeviceId(deviceDetails.getId());
        deviceYields.setYieldsCounting(deviceYieldVo.getYield());
        if (Objects.nonNull(deviceYieldVo.getIsProductGood())) {
            deviceYields.setIsProductGood(deviceYieldVo.getIsProductGood());
        }else {
            deviceYields.setIsProductGood(ProductGoodType.GOOD_TYPE.getNumber());
        }
        deviceYields.setCreateBy("10101010");
        deviceYields.setUpdateBy("10101010");
        deviceYields.setCreateTime(new Date());
        deviceYields.setUpdateTime(new Date());
        deviceYields.setIsDeleted(0);
        deviceYields.setCountAll(deviceYieldVo.getCountAll());
        iDeviceYieldsService.insertDeviceYields(deviceYields);
    }
}
